class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        PriorityQueue<Pair<Integer,Integer>> queue = new PriorityQueue<Pair<Integer,Integer>>((a,b)->{
            return b.getValue() - a.getValue();
        });
        int length = nums.length;
        int[] ans = new int[length-k+1];
        for(int i=0; i<nums.length; i++){
            queue.offer(new Pair<Integer,Integer>(i, nums[i]));
            if(i>=k-1){
                while(queue.peek().getKey()< i-k+1){
                    queue.poll();
                }
                int value = queue.peek().getValue();
                ans[i-k+1] = value;
            } 
        }
        return ans;
    }
}